package me.ilich.piet;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import me.ilich.piet.interpreter.JPietInterpreter;

public final class JPiet {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("JPiet");
		System.out.println("Dmitry Sokolov, 2012");
		if(args==null || args.length==0){
			noArgs();
		}else if(args.length==1){
			String arg1 = args[0];
			executeProgram(arg1);
		}else{
			tooMuchArgs();
		}		
		System.out.println("done");
		
	}
	
	private static void noArgs(){
		System.out.println("file name not specified");
	}
	
	private static void tooMuchArgs(){
		System.out.println("too much arguments");
	}
	
	private static void executeProgram(String fileName){
		final JPietInterpreter interpreter = new JPietInterpreter();
		final File file = new File(fileName); 
		InputStream stream = null;
		try {
			stream = new FileInputStream(file);
			interpreter.run(stream);
		} catch (FileNotFoundException e) {
			System.out.println("file " + fileName + " not found");
		} catch (JPietInterpreter.InterpreterException e) {
			System.out.println("InterpreterException " + e.getMessage());
		} finally {
			if(stream!=null){
				try {
					stream.close();
				} catch (IOException e) {}
			}
		}
	}

}
